package org.aeon.foa.app;

import org.aeon.foa.entity.Meals;
import org.aeon.foa.entity.Order;
import org.aeon.foa.util.DataUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @projectName: easytaskkit
 * @package: org.aeon.foa.app
 * @className: App
 * @author: MissingAeon
 * @date: 2024/1/2 15:13
 * @description:
 */
public class App {
    private static final Scanner in;
    private static final List<Meals> meals;
    private static final List<Order> orders;

    static {
        in = new Scanner(System.in);
        meals = new ArrayList<>();
        meals.add(new Meals(1, "红烧带鱼", 38));
        meals.add(new Meals(meals.size() + 1, "鱼香肉丝", 20));
        meals.add(new Meals(meals.size() + 1, "时令鲜蔬", 38));
        orders = new ArrayList<>();
    }


    public void startUp() {
        System.out.println("欢迎使用“吃货联盟订餐系统”");
        while (true) {
            Menu.menu();
            int choice = in.nextInt();
            switch (choice) {
                case 1 -> orderingMeals();
                case 2 -> Cart();
                case 3 -> signFor();
                case 4 -> delOrder();
                case 5 -> like();
                case 6 -> System.exit(0);
            }
        }
    }

    private void like() {
        int choice;
        System.out.println("***我要点赞***");
        Menu.likePage(meals);
        System.out.print("请选择您要点赞的菜品序号:");
        choice = in.nextInt();
        if (choice > meals.size() || choice < 1) {
            System.out.println("您选择的订单不存在!");
            Menu.fallback(in);
            return;
        }
        Meals meals0 = meals.get(choice - 1);
        meals0.setLike(meals0.getLike() + 1);
        System.out.println("点赞成功!");
        Menu.fallback(in);
    }

    private void delOrder() {
        int choice;
        System.out.println("***删除订单***");
        System.out.print("请输入要删除的订单序号:");
        choice = in.nextInt();
        if (choice > orders.size() || choice < 1) {
            System.out.println("您选择的订单不存在!");
            Menu.fallback(in);
            return;
        }
        Order order = orders.get(choice - 1);
        if (!order.getState()) {
            System.out.println("您选择的订单未签收，不能删除!");
            Menu.fallback(in);
            return;
        }
        System.out.println("删除订单成功!");
        Menu.fallback(in);
    }

    private void signFor() {
        int choice;
        System.out.println("***签收定单***");
        System.out.println("请选择要签收的订单序号:");
        choice = in.nextInt();
        if (choice > orders.size() || choice < 1) {
            System.out.println("您选择的订单不存在!");
            Menu.fallback(in);
            return;
        }
        Order order = orders.get(choice - 1);
        if (order.getState()) {
            System.out.println("您选择的订单已完成签收，不能再次签收!");
            Menu.fallback(in);
            return;
        }
        order.setState(true);
        System.out.println("订单签收成功!");
        Menu.fallback(in);
    }

    private void Cart() {
        System.out.println("***查看餐袋***");
        Menu.mealBagPage(orders);
        Menu.fallback(in);
    }

    private void orderingMeals() {
        System.out.println("***我要订餐***");
        System.out.print("请输入姓名:");
        String name = in.next();
        Menu.orderingPage(meals);
        System.out.print("请选择您要点的菜品编号:");
        int number = in.nextInt();
        Meals meals0 = meals.get(number - 1);
        System.out.print("请选择您需要的份数:");
        int numberCopies = in.nextInt();
        int time;
        do {
            System.out.print("请输入送餐时间(送餐时间是10点至20点间整点送餐):");
            time = in.nextInt();
        } while (time <= 10 || time >= 20);
        System.out.print("请输入送餐地址:");
        String address = in.next();
        Order order = new Order(orders.size() + 1, name, meals0.getDishName() + numberCopies + "份",
                time, DataUtil.getDay(), address, meals0.getUnitPrice() * numberCopies, 6);
        System.out.println("订餐成功!");
        orders.add(order);
        Menu.orderSettlement(order);
        Menu.fallback(in);
    }
}
